package com.sr.examination.exam.exercise.category;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.sr.examination.R;
import com.tl.commonlibrary.ui.beans.ExerciseBean;
import com.tl.commonlibrary.ui.beans.ExerciseListBean;
import com.tl.commonlibrary.ui.beans.ExerciseOption;

import java.util.List;

/**
 * Created by jiangzhongshan on 18/2/3.
 * 单选
 */

public class JudgementChoiceFragment extends ChoiceFragment {
    public static JudgementChoiceFragment newInstance(ExerciseBean exerciseBean, int examMethod) {
        JudgementChoiceFragment fragment = new JudgementChoiceFragment();
        fragment.setExerciseBean(exerciseBean);
        fragment.setExamMethod(examMethod);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        exerciseType = ExerciseBean.TYPE_SINGLE_CHOOSE;
        super.onCreateView(inflater, container, savedInstanceState);
        choiceListView.setChoiceMode(exerciseBean.isAnswered() ? AbsListView.CHOICE_MODE_NONE : AbsListView.CHOICE_MODE_SINGLE);
        setDescription(getString(R.string.exercise_judgement_title_content, exerciseBean.getScore(), exerciseBean.getItemQuest()));
        return root;
    }

    @Override
    protected void initOption() {
        String userAnswer = exerciseBean.getUserAnswer();
        if (!TextUtils.isEmpty(userAnswer)) {
            List<ExerciseOption> options = exerciseBean.getOptions();
            exerciseBean.setAnswered(true);
            if (options != null && !options.isEmpty()) {
                for (ExerciseOption option : options) {
                    if (userAnswer.equals(option.getId())) {
                        option.setChecked(true);
                        break;
                    }
                }
            }
            if (examMethod == ExerciseListBean.METHOD_PRACTICE) {
                //答案错误，显示解析
                if (!userAnswer.equals(exerciseBean.getAnswer())) {
                    onExerciseDisplayModeChanged(ExerciseBean.DISPLAY_MODE_ANALYSIS);
                }
            }
        }
    }

    @Override
    protected void onExerciseAnalysisModeCalled() {
        super.onExerciseAnalysisModeCalled();
    }

    @Override
    protected void onExerciseAnsweringModeCalled() {
        super.onExerciseAnsweringModeCalled();
    }

    @Override
    protected void onClickOption(List<ExerciseOption> options) {
        /**
         * 练习模式下，答错显示解析，正确进入下一题
         * 考试模式下，都进入下一题
         */
        if (examMethod == ExerciseListBean.METHOD_PRACTICE) {
            //单选选中之后如果答案正确，进入下一题，答案错误，显示解析
            if (options.get(0).getId().equals(exerciseBean.getAnswer())) {
                Bundle bundle = new Bundle();
                bundle.putBoolean("answer", true);
                onFragmentTriggerd(exerciseType, bundle);
            } else {
                onExerciseDisplayModeChanged(ExerciseBean.DISPLAY_MODE_ANALYSIS);
            }
        } else if (examMethod == ExerciseListBean.METHOD_EXAMINATION) {
            Bundle bundle = new Bundle();
            bundle.putBoolean("answer", true);
            onFragmentTriggerd(exerciseType, bundle);
        }
    }


    @Override
    public void onClick(View v) {

    }
}
